#include <iostream>
#include <vector>
#include "framework/logger.h"
#include "framework/linear_allocator.h"
#include "framework/resource_xml.h"
#include "framework/resource_cache.h"
#include "renderer/cmdbuf.h"
#include "renderer/vertex_layout.h"

int main( int argc, char* argv[] )
{
	cyb::ResourceCache<cyb::XmlResource> xmlCache( "XMLFile cache" );
	cyb::g_log->Reset( xmlCache.Add( "resources/logging.xml" ) );

	//=========== Linear allocator test
	cyb::LinearAllocator allocator( 3000 );
	allocator.Clear();
	char *buf1 = (char *) allocator.Allocate( 241 );
	char *buf2 = (char *) allocator.Allocate( 12 );
	char *buf3 = (char *) allocator.Allocate( 333 );

	//=========== SortKey test
	cyb::SortKey sortKey;
	sortKey.m_depth = 1123;
	sortKey.m_program = 55;
	sortKey.m_translucecy = 3;
	sortKey.m_layer = 1;
	sortKey.m_viewport = 0;

	uint64_t key = sortKey.Encode();
	sortKey.Decode( key );

	//=========== Vertex layout test
	cyb::VertexLayout vtxLayout( {
		cyb::VertexLayout::Entry( cyb::VertexLayout::Attribute::Position, 3, cyb::VertexLayout::Type::Float ),
		cyb::VertexLayout::Entry( cyb::VertexLayout::Attribute::Color0,   4, cyb::VertexLayout::Type::Uint8, true)
	} );

	vtxLayout.Print();

 	//=========== Resource test
	cyb::ResourceCache<cyb::Resource> testCache( "fruit cache" );
	std::vector<std::shared_ptr<cyb::Resource>> resources;

	resources.push_back( testCache.Add( "Apple" ) );
	resources.push_back( testCache.Add( "Apple" ) );
	resources.push_back( testCache.Add( "Banana" ) );
	resources.push_back( testCache.Add( "Mango" ) );
	resources.push_back( testCache.Add( "Grape" ) );
	resources.push_back( testCache.Add( "Apple" ) );

	CYB_INFO( testCache.UsageString() );

	CYB_INFO( "Popping last 3..." );
	resources.pop_back();
	resources.pop_back();
	resources.pop_back();

	CYB_WARNING( false, "Removing unused resources..." );
	testCache.RemoveUnused();
	CYB_INFO( testCache.UsageString() );

 	return 0;
}